{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "vscode": {
     "languageId": "plaintext"
    }
   },
   "source": [
    "字符串的局部编辑API,使用切片语法会非常的方便。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "# s.erase(s.begin());  // 移除字符串s的第一个字符\n",
    "def the_first_char():\n",
    "    first_string = \"Hello\"\n",
    "    first_string_result = first_string[1:]  # 这将移除字符串s的第一个字符\n",
    "    print(first_string_result)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "ello\n"
     ]
    }
   ],
   "source": [
    "the_first_char()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "如何在已经有的字符串前面增加重复的部分呢？"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 拼接两个字符串\n",
    "def the_concat_usecase():\n",
    "    string_A = \"hello\"\n",
    "    string_B = \"world\"\n",
    "    print(string_A + string_B)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "helloworld\n"
     ]
    }
   ],
   "source": [
    "the_concat_usecase()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 构成一个重复字符串\n",
    "def repetitive_string_usecase():\n",
    "    char = \"a\"\n",
    "    print(char * 10)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "aaaaaaaaaa\n"
     ]
    }
   ],
   "source": [
    "repetitive_string_usecase()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Python的String是不可变对象，每个API返回的都是新的对象。\n",
    "在 C++ 中，std::string 是一个可变的字符串类型，所以它与 Python 中不可变的字符串（str）有很大不同。C++ 中的 std::string 类允许修改字符串内容，包括删除字符。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "String可以由Char拼接吗？"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "source": [
    "def concat_a_string():\n",
    "    a,b,c,d,e,f = '1','2','3','4','5','6'\n",
    "    string_number = a+b+c+d+e+f\n",
    "    print(\"the value of string is,\",string_number,\"the type is ,\",type(string_number))\n",
    "    return string_number"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "the value of string is, 123456 the type is , <class 'str'>\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "'123456'"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "concat_a_string()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "String可以转化为数字吗？"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [],
   "source": [
    "def string_2_num():\n",
    "    string_number = concat_a_string()\n",
    "    my_number = int(string_number)\n",
    "    print(\"the value of my_number is,\",my_number,\"the type is ,\",type(my_number))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "the value of string is, 123456 the type is , <class 'str'>\n",
      "the value of my_number is, 123456 the type is , <class 'int'>\n"
     ]
    }
   ],
   "source": [
    "string_2_num()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "science39",
   "language": "python",
   "name": "science39"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.18"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
